package burubaka.service;

import java.util.List;

import org.slim3.datastore.Datastore;

import burubaka.meta.PreRaceInfoMeta;
import burubaka.model.PreRaceInfo;

import com.google.appengine.api.datastore.Transaction;

/**
 * @author tk
 * 
 */
public class PreRaceInfoService {

	private PreRaceInfoMeta meta = new PreRaceInfoMeta();

	/**
	 * 1件検索
	 * 
	 * @return PreRaceInfo
	 */
	public PreRaceInfo find() {
		return Datastore.query(meta).filter(meta.execFlag.equal(0)).sort(meta.raceDate.asc).offset(0).limit(1)
				.asSingle();
	}

	/**
	 * 全件検索
	 * 
	 * @return List<PreRaceInfo>
	 */
	public List<PreRaceInfo> findAll() {
		return Datastore.query(meta).sort(meta.raceDate.asc).asList();
	}

	/**
	 * リミット指定検索
	 * 
	 * @return PreRaceInfo
	 */
	public List<PreRaceInfo> find(int limit) {
		return Datastore.query(meta).filter(meta.execFlag.equal(0)).sort(meta.raceDate.asc).offset(0).limit(limit)
				.asList();
	}

	/**
	 * 登録
	 * 
	 * @param preRaceInfo
	 * @return boolean
	 */
	public boolean regist(PreRaceInfo preRaceInfo) {

		Transaction tx = Datastore.beginTransaction();
		Datastore.put(preRaceInfo);
		tx.commit();

		return true;
	}

	/**
	 * 登録
	 * 
	 * @param preRaceInfoList
	 * @return boolean
	 */
	public boolean regist(List<PreRaceInfo> preRaceInfoList) {

		// Transaction tx = Datastore.beginTransaction();
		Datastore.delete(Datastore.query(meta).asKeyList());
		Datastore.put(preRaceInfoList);
		// tx.commit();

		return true;
	}

	/**
	 * 全件削除
	 * 
	 * @return boolean
	 */
	public boolean deleteAll() {

		Transaction tx = Datastore.beginTransaction();
		Datastore.delete(Datastore.query(meta).asKeyList());
		tx.commit();
		return true;
	}

}
